package design.pattern.singleton.register;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

// 是非线程安全的

/**
 * @description: 注册式单例-容器缓存
 * @author: liuzhangcheng
 **/
public class ContainerSingleton {

    private static Map<String, Object> ioc = new ConcurrentHashMap<>();

    private ContainerSingleton() {}

    public static Object getInstance(String className) {
        synchronized (ioc) {
            if (!ioc.containsKey(className)) {
                Object object = null;
                try {
                    object = Class.forName(className).newInstance();
                    ioc.put(className, object);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return object;
            }

            return ioc.get(className);
        }
    }

}
